package pool

func NewPoolMap() (obj *PoolMap) {
	obj = &PoolMap{
		data: map[string]*Pool{},
	}
	return
}

type PoolMap struct {
	data map[string]*Pool
}

func (m *PoolMap) getPool(key string) *Pool {
	p, ok := m.data[key]
	if ok == false {
		p = NewPool()
		m.data[key] = p
	}

	return p
}
func (m *PoolMap) Count(key string) int {
	return m.getPool(key).count
}
func (m *PoolMap) Dispose() {
	for _, val := range m.data {
		val.Dispose()
	}

	m.data = nil
}

func (m *PoolMap) Get(key string) interface{} {
	return m.getPool(key).GetItem()
}

func (m *PoolMap) GetOrNew(key string, args ...interface{}) interface{} {
	return m.getPool(key).GetOrNewItem(args...)
}

func (m *PoolMap) Put(key string, item interface{}) {
	m.getPool(key).PutItem(item)
}

func (m *PoolMap) RegisterPool(key string, creator func(...interface{}) interface{}) {
	p := m.getPool(key)
	p.creator = creator
}
